package com.desgin.service;

import com.common.ResponseVO;
import com.desgin.domain.dto.ModelDTO;
import com.desgin.enums.ModelEnum;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * @author zhaofang
 * @version 1.0
 * @since 2021/8/13 14:29
 **/

@Service
public class AdapterModelService implements ModelServiceI {

    @Autowired
    private Map<String, ModelTemplateServiceI> modelTemplateServiceIMap;

    @Override
    public ResponseVO<String> getMessage(ModelDTO modelDTO) {
        Pair<Boolean, ModelTemplateServiceI> modelPair = this.findProjectService(modelDTO.getModelCode());
       if (!modelPair.getLeft()){
            return ResponseVO.build(ModelEnum.respCode.ERROR.getCode(),ModelEnum.respCode.ERROR.getMsg(),null);
       }
        return modelPair.getRight().getMessage(modelDTO);
    }

    private Pair<Boolean, ModelTemplateServiceI> findProjectService(String modelCode) {
        if (!StringUtils.hasText(modelCode)) {
            return Pair.of(false, null);
        }
        String key = "modelService" + modelCode;
        if (ObjectUtils.isEmpty(modelTemplateServiceIMap) || ObjectUtils.isEmpty(modelTemplateServiceIMap.get(key))) {
            return Pair.of(false, null);
        }

        return Pair.of(true, modelTemplateServiceIMap.get(key));
    }
}